import _ from 'underscore'

import { merge } from '../../helper/helper'

// Reducer
const defaultState = {
    socialAccount: {},
    avatar: user.headImage,
    userName: user.name,
    nickName: user.nickName,
    email: user.email,
    oldPassword: '',
    newPassword: '',
    newPasswordRepeat: '',
    paymentList: {
        isPending: false,
        skip: 0,
        chosen: [0, 1, 2],
        data: [],
        count: 0
    },
    errors: [],
    alert: '',
    showAlert: false,
    loading: false
};

function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'set-socialaccount':
            return merge(state, {
                socialAccount: action.val
            });
        case 'set-errors':
            return merge(state, {
                errors: action.val
            })
        case 'set-avatar':
            return merge(state, {
                avatar: action.val
            });
        case 'set-username':
            return merge(state, {
                userName: action.val
            });
        case 'set-nickname':
            return merge(state, {
                nickName: action.val
            });

        case 'set-email':
            return merge(state, {
                email: action.val
            });
        case 'set-oldPassword':
            return merge(state, {
                oldPassword: action.val
            });
        case 'set-newPassword':
            return merge(state, {
                newPassword: action.val
            });
        case 'set-newPasswordRepeat':
            return merge(state, {
                newPasswordRepeat: action.val
            });

        case 'payment-list-request':
            return merge(state, {
                paymentList: merge(state.paymentList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'payment-list-render-data':
            return merge(state, {
                paymentList: merge(state.paymentList, {
                    isPending: false,
                    data: action.val.data,
                    count: action.val.count
                })
            });
        case 'payment-list-set-chosen':
            return merge(state, {
                paymentList: merge(state.paymentList, {
                    chosen: action.val
                })
            });
        case 'change-loading-status':
            return merge(state, {
                loading: !state.loading
            })

    }

    return state
}

export default reducer